package cn.edu.sdjzu.xg.util;

import java.util.List;

public class Condition {
    public static String toWhereClause(List<Condition> conditionList) {
        StringBuilder sb = new StringBuilder(" WHERE ");
        for (Condition c : conditionList) {
            sb.append(c.getKey()).append(" ").append(c.getOperator()).append(" ");
            String value = c.getValue();
            if ("like".equalsIgnoreCase(c.getOperator())) {
                value = "%" + value + "%";
                value = TxtHelper.quoteMarked(value);
            }
            sb.append(value);
            sb.append(" AND ");
        }
        TxtHelper.truncEnding(sb, 5);
        return sb.toString();
    }

    public Condition(String key, String operator, String value) {
        this.key = key;
        this.operator = operator;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getOperator() {
        return operator;
    }

    public String getValue() {
        return value;

    }

    @Override
    public String toString() {
        return "Condition{" +
                "key='" + key + '\'' +
                ", operator='" + operator + '\'' +
                ", value='" + value + '\'' +
                '}';
    }

    private String key;
    private String operator;
    private String value;

}

